package cn.icare.cms.impl;

import cn.icare.cms.ItemCatService;
import cn.icare.cms.ListUtil;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.ItemCatMapper;
import cn.icare.cms.dao.po.ItemCat;
import cn.icare.cms.model.ro.AddItemCatRO;
import cn.icare.cms.model.ro.EditItemCatRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Resource
    private ItemCatMapper itemCatMapper;

    private final Logger logger = LoggerFactory.getLogger(ItemCatServiceImpl.class);

    @Override
    public Long createOne(AddItemCatRO ro) {

        ItemCat model = new ItemCat();
        model.setParentid(ro.getParentId());
        model.setName(ro.getName());
        model.setSortindex(ro.getSortIndex());
        model.setCreatetime(new Date());
        if (ro.getParentId() > 0) {
            if(StringUtil.isNotEmpty(ro.getParentPath())) {
                model.setPath(ro.getParentPath() + "$" + ro.getParentId());
            }
            else
            {
                model.setPath(""+ro.getParentId());
            }
        }
        Integer result = this.itemCatMapper.insertSelective(model);
        if (result > 0) {
            return model.getId();
        }
        return 0L;
    }

    @Override
    public boolean updateOne(EditItemCatRO ro) {

        ItemCat model = new ItemCat();
        model.setId(ro.getId());
        model.setName(ro.getName());
        model.setSortindex(ro.getSortIndex());
        model.setUpdatetime(new Date());
        Integer result = this.itemCatMapper.updateByPrimaryKeySelective(model);
        return result > 0;
    }

    @Override
    public boolean deleteOne(Long id) {
        return this.itemCatMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(ItemCat.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.itemCatMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public ItemCat getOne(Long id) {
        return this.itemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(Long parentId, String name, Integer pageNum, Integer pageSize) {

        MsgDatasVO result = new MsgDatasVO();
        try {
            if (pageSize == 0) {
                Example example = new Example(ItemCat.class);
                Example.Criteria criteria = example.createCriteria();
                if (StringUtil.isNotEmpty(name)) {
                    criteria.andLike("name", "%" + name + "%");
                }
                if (parentId > -1) {
                    criteria.andEqualTo("parentid", parentId);
                }
                example.setOrderByClause("sortindex asc");
                List<ItemCat> dataList = this.itemCatMapper.selectByExample(example);
                result.setTotal(dataList.size());
                result.setDatas(dataList);

            }else {
                PageHelper.startPage(pageNum, pageSize);
                Example example = new Example(ItemCat.class);
                Example.Criteria criteria = example.createCriteria();
                if (StringUtil.isNotEmpty(name)) {
                    criteria.andLike("name", "%" + name + "%");
                }
                if (parentId > -1) {
                    criteria.andEqualTo("parentid", parentId);
                }
                example.setOrderByClause("sortindex asc");
                List<ItemCat> dataList = this.itemCatMapper.selectByExample(example);
                PageInfo pageInfo = new PageInfo(dataList);
                result.setTotal(pageInfo.getTotal());
                result.setDatas(dataList);
            }
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;

    }

    @Override
    public boolean isExists(Long id ,Long parentId, String name) {

        Example example = new Example(ItemCat.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        if (parentId>-1) {
            criteria.andEqualTo("parentid", parentId);
        }
        criteria.andNotEqualTo("id",id);

        List<ItemCat> dataList = this.itemCatMapper.selectByExample(example);
        if(dataList!=null && dataList.size()>0)
        {
            return true;
        }
        return false;
    }
}
